import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { retry } from 'rxjs/operators';

export interface Article {
  status: number;
  publish_at: string | null;
  _id: string;
  title: string;
  author: {
    name: string;
    _id: string;
  };
  content: string;
  create_at: string;
  update_at: string;
}

interface BaseResponse<T> {
  result: T;
  total?: number;
}

interface ArticlesQuery {
  status?: number;
  sort?: number;
  userId?: string;
}

@Injectable({
  providedIn: 'root',
})
export class ArticleService {
  constructor(private http: HttpClient) {}

  getArticles(current: number, limit: number, query?: ArticlesQuery) {
    const params = {
      current,
      limit,
      ...query,
    };

    return this.http.get<BaseResponse<Article[]>>('/articles', {
      params: <any>params,
    });
  }
}
